<?php

declare (strict_types=1);

namespace App\Application\Admin\Model;

use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Db;
use Hyperf\DbConnection\Model\Model;

/**
 * @property int $package_id
 * @property string $name
 * @property float $price
 * @property int $time
 * @property int $status
 * @property int $store_id
 * @property int $admin_id
 * @property int $type
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property string $deleted_at
 * @property-read \Hyperf\Database\Model\Collection|self[] $children
 */
class Package extends Model
{
    use SoftDeletes;

    protected string $primaryKey = 'package_id';
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected ?string $table = 'package';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected array $fillable = ['package_id', 'name', 'price', 'time', 'status', 'store_id', 'admin_id', 'type'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected array $casts = [
        'package_id' => 'integer',
        'name' => 'string',
        'price' => 'float',
        'time' => 'integer',
        'status' => 'integer',
        'store_id' => 'integer',
        'admin_id' => 'integer',
        'type' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];


    /**
     * 创建门店
     *
     * @param string $store_name
     * @param string $password
     * @param string $real_name
     * @param int $role_id
     * @return bool
     * @throws ErrorException
     */
    static function createPackage(int $package_id, array $store = []): self
    {
        Db::beginTransaction();
        $store = self::updateOrCreate(['package_id' => $package_id], $store);
        Db::commit();
        return $store;

    }

}